<?php
/**
 * 飞信机器人--事件插件--同步好友数据
 * 功能： 传送好友信息数据
 * 作者： Kelvin
 * 联系： msn:shichangguo@msn.com, qq:191848423
*/
require_once('./plugins/inc/config.inc.php');
require_once('./plugins/inc/utils.inc.php');
require_once('./plugins/inc/db.inc.php');

$srvMobile = $argv[1];
$parameters = base64_decode($argv[2]);	//编码过的好友信息

$p = xml_parser_create();
xml_parse_into_struct($p, $parameters, $valsArr, $indexArr);
xml_parser_free($p);

$buddyUserID="";
$buddyType="";
$buddyInReverseList="";
$buddyURI="";
$buddySID="";
$buddySEG="";
$buddyMobile="";
$buddyStatusCode="";
$buddyOnlineStatus="";
$buddyOnlineDesc="";
$buddyNickName="";
$buddyLocalName="";
$buddyImpresa="";

for($i=0; $i<count($valsArr); $i++)
{
	switch($valsArr[$i]['tag'])
	{
		case 'TYPE':$buddyType = isset($valsArr[$i]['value'])?$valsArr[$i]['value']:-1;
					break;
		case 'USER-ID':$buddyUserID = isset($valsArr[$i]['value'])?$valsArr[$i]['value']:-1;
					break;
		case 'IN-REVERSE-LIST':$buddyInReverseList = isset($valsArr[$i]['value'])?$valsArr[$i]['value']:-1;
					break;
		case 'URI':$buddyURI = isset($valsArr[$i]['value'])?$valsArr[$i]['value']:-1;
					break;
		case 'SID':$buddySID = isset($valsArr[$i]['value'])?$valsArr[$i]['value']:-1;
					break;
		case 'SEG':$buddySEG = isset($valsArr[$i]['value'])?$valsArr[$i]['value']:-1;
					break;
		case 'MOBILE':$buddyMobile = isset($valsArr[$i]['value'])?$valsArr[$i]['value']:-1;
					break;
		case 'STATUS-CODE':$buddyStatusCode = isset($valsArr[$i]['value'])?$valsArr[$i]['value']:-1;
					break;
		case 'ONLINE-STATUS':$buddyOnlineStatus = isset($valsArr[$i]['value'])?$valsArr[$i]['value']:-1;
					break;
		case 'ONLINE-DESC':$buddyOnlineDesc = isset($valsArr[$i]['value'])?base64_decode($valsArr[$i]['value']):-1;
					break;
		case 'NICKNAME':$buddyNickName = isset($valsArr[$i]['value'])?base64_decode($valsArr[$i]['value']):-1;
					break;
		case 'LOCALNAME':$buddyLocalName = isset($valsArr[$i]['value'])?base64_decode($valsArr[$i]['value']):-1;
					break;
		case 'IMPRESA':$buddyImpresa = isset($valsArr[$i]['value'])?base64_decode($valsArr[$i]['value']):-1;
					break;					
	}
}

$db = new CDb($srvMobile);
/* init database */
$sql = "select * from fx_config where fx_config.key='mobile'";
$retArr = $db->fetchQueryAll($sql);
if( count($retArr) == 1)
{
        $v = $retArr[0]['value'];
        if( $v != $srvMobile )
        {
                $db->execute('truncate table fx_participate');
                $db->execute('truncate table fx_guestbook');
                $db->execute('truncate table fx_buddies');
                $db->execute("update fx_config set fx_config.value='{$srvMobile}' where fx_config.key='mobile'");
        }
}
/* init database */
$retArr = $db->fetchQueryAll("select * from fx_buddies where uri='$buddyURI'");
if( count($retArr) == 0 )
{
		$sql = "insert into fx_buddies(buddy_type,user_id,uri,sid,seg,mobile,reverse,status_code,online_status,online_desc,nick_name,local_name,impresa,last_update_time)";
		$sql.= "values('{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}')";
		$sql = str_replace('{1}', $buddyType!=-1?$buddyType:0,$sql);
		$sql = str_replace('{2}', $buddyUserID!=-1?$buddyUserID:0,$sql);
		$sql = str_replace('{3}', $buddyURI!=-1?$buddyURI:'',$sql);
		$sql = str_replace('{4}', $buddySID!=-1?$buddySID:'',$sql);
		$sql = str_replace('{5}', $buddySEG!=-1?$buddySEG:'',$sql);
		$sql = str_replace('{6}', $buddyMobile!=-1?$buddyMobile:'',$sql);
		$sql = str_replace('{7}', $buddyInReverseList!=-1?$buddyInReverseList:0,$sql);
		$sql = str_replace('{8}', $buddyStatusCode!=-1?$buddyStatusCode:0,$sql);
		$sql = str_replace('{9}', $buddyOnlineStatus?$buddyOnlineStatus:0,$sql);
		$sql = str_replace('{10}', $buddyOnlineDesc!=-1?$buddyOnlineDesc:'',$sql);
		$sql = str_replace('{11}', $buddyNickName!=-1?$buddyNickName:'',$sql);
		$sql = str_replace('{12}', $buddyLocalName!=-1?$buddyLocalName:'',$sql);
		$sql = str_replace('{13}', $buddyImpresa!=-1?$buddyImpresa:'',$sql);
		$sql = str_replace('{14}', time(NULL),$sql);		
}else
{
	$sql = "update fx_buddies set buddy_type='{1}',user_id='{2}',uri='{3}',sid='{4}',seg='{5}',mobile='{6}',reverse='{7}',status_code='{8}',online_status='{9}',online_desc='{10}',nick_name='{11}',local_name='{12}',impresa='{13}' where id=".$retArr[0]['id'];		
	$sql = str_replace('{1}', $buddyType!=-1?$buddyType:$retArr[0]['buddy_type'],$sql);
	$sql = str_replace('{2}', $buddyUserID!=-1?$buddyUserID:$retArr[0]['user_id'],$sql);
	$sql = str_replace('{3}', $buddyURI!=-1?$buddyURI:$retArr[0]['uri'],$sql);
	$sql = str_replace('{4}', $buddySID!=-1?$buddySID:$retArr[0]['sid'],$sql);
	$sql = str_replace('{5}', $buddySEG!=-1?$buddySEG:$retArr[0]['seg'],$sql);
	$sql = str_replace('{6}', $buddyMobile!=-1?$buddyMobile:$retArr[0]['mobile'],$sql);
	$sql = str_replace('{7}', $buddyInReverseList!=-1?$buddyInReverseList:$retArr[0]['reverse'],$sql);
	$sql = str_replace('{8}', $buddyStatusCode!=-1?$buddyStatusCode:$retArr[0]['status_code'],$sql);
	$sql = str_replace('{9}', $buddyOnlineStatus!=-1?$buddyOnlineStatus:$retArr[0]['online_status'],$sql);
	$sql = str_replace('{10}', $buddyOnlineDesc!=-1?$buddyOnlineDesc:$retArr[0]['online_desc'],$sql);
	$sql = str_replace('{11}', $buddyNickName!=-1?$buddyNickName:$retArr[0]['nick_name'],$sql);
	$sql = str_replace('{12}', $buddyLocalName!=-1?$buddyLocalName:$retArr[0]['local_name'],$sql);
	$sql = str_replace('{13}', $buddyImpresa!=-1?$buddyImpresa:$retArr[0]['impresa'],$sql);
}
$db->execute($sql);
?>
